package com.vhall.component.controller.vote;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ReUtil;
import com.vhall.component.controller.room.facade.RoomInnerFacade;
import com.vhall.component.controller.vote.facade.VoteConsoleFacade;
import com.vhall.component.entity.page.vo.PageVO;
import com.vhall.component.entity.room.dto.RoomsEntityDTO;
import com.vhall.component.entity.vote.vo.*;
import com.vhall.component.entity.vote.vo.console.*;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.annotation.TokenAuth;
import com.vhall.component.plugin.common.constant.ConstStr;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.plugin.common.support.Token;
import com.vhall.component.plugin.common.support.TokenThreadLocal;
import com.vhall.component.service.vote.VoteConsoleService;
import com.vhall.component.service.vote.VoteConstants;
import com.vhall.component.service.vote.VoteService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author longzhen.gong
 * @date 2021/7/6 11:43
 */
@VhallComponent
@TokenAuth
public class VoteConsoleController implements VoteConsoleFacade {

    @Autowired
    private RoomInnerFacade roomInnerFacade;

    @Autowired
    private VoteService voteService;

    @Autowired
    private VoteConsoleService voteConsoleService;


    @Override
    public VotesCreateRspVO create(VotesCreateConsoleReqVO reqVO) {
        RoomsEntityDTO roomsEntityDTO = null;
        if (Objects.nonNull(reqVO.getIlId())) {
            roomsEntityDTO = roomInnerFacade.getRoomInfoByIlId(reqVO.getIlId() + "");
        }
        String roomId = roomsEntityDTO == null ? "" : roomsEntityDTO.getRoomId();
        reqVO.setRoomId(roomId);
        Token token = TokenThreadLocal.getToken();
        reqVO.setAccountId(token.getAccountId());
        VotesCreateReqVO votesCreateReqVO = new VotesCreateReqVO();
        BeanUtils.copyProperties(reqVO, votesCreateReqVO);
        votesCreateReqVO.setLimitTime(Integer.valueOf(reqVO.getLimitTime()));
        return voteConsoleService.create(votesCreateReqVO);
    }

    @Override
    public List<Long> delete(VoteDeleteConsoleReq req) {
        if (!ReUtil.isMatch(VoteConstants.REG_IDS, req.getIds())) {
            throw new BusinessException(BizErrorCode.BIZ_INVALID_PARAM);
        }
        List<Long> voteIds = Arrays.stream(req.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        Token token = TokenThreadLocal.getToken();
        Integer accountId = token.getAccountId();
        return voteConsoleService.delete(voteIds, accountId);
    }

    @Override
    public VotesUpdateRspVO update(VotesUpdateConsoleRepVO repVO) {
        Token token = TokenThreadLocal.getToken();
        Integer accountId = token.getAccountId();
        return voteConsoleService.update(repVO, accountId);
    }

    @Override
    public PageVO<VotesListRspVO> list(VotesListConsoleReqVO reqVO) {
        if (reqVO.getIlId() == null) {
            reqVO.setIlId(0);
        }
        Integer ilId = reqVO.getIlId();
        Token token = TokenThreadLocal.getToken();
        Integer accountId = token.getAccountId();
        VotesListReqVO votesListReqVO = new VotesListReqVO();
        BeanUtils.copyProperties(reqVO, votesListReqVO);
        votesListReqVO.setAccountId(accountId + "");
        if (!ilId.equals(0)) {
            RoomsEntityDTO roomInfo = roomInnerFacade.getRoomInfoByIlId(reqVO.getIlId().toString());
            if (roomInfo == null) {
                throw new BusinessException(BizErrorCode.EMPTY_ROOM);
            }
            votesListReqVO.setRoomId(roomInfo.getRoomId());
            votesListReqVO.setAccountId(null);
        }
        return voteConsoleService.list(votesListReqVO, accountId);
    }

    /**
     * 投票-获取房间下投票信息 是否回答过
     *
     * @param voteInfoVO
     * @return
     */
    @Override
    public VoteInfoRspVO info(VoteInfoReqVO voteInfoVO) {
        Token token = TokenThreadLocal.getToken();
        Integer accountId = token.getAccountId();
        return voteConsoleService.info(voteInfoVO, accountId);

    }

    /**
     * 投票统计详情
     *
     * @param reqVO
     * @return
     */
    @Override
    public PageVO<StatRsqVO> stat(StatReqVO reqVO) {
        String startDate = reqVO.getBeginTime();
        // 消息服务要求精确到时分秒
        if (StringUtils.isNotBlank(startDate)) {
            // 判断格式
            if (startDate.length() == 10) {
                startDate = startDate + " " + ConstStr.TIME_ZERO;
            }
        } else {
            startDate = ConstStr.DATE_TIME_1970;
        }
        reqVO.setBeginTime(startDate);
        String endDate = reqVO.getEndTime();
        if (StringUtils.isNotBlank(endDate)) {
            // 判断格式
            if (endDate.length() == 10) {
                endDate = endDate + " " + ConstStr.TIME_FULL;
            }
        } else {
            Date now = new Date();
            endDate = DateUtil.format(now, DatePattern.NORM_DATETIME_PATTERN);
        }
        reqVO.setEndTime(endDate);
        if (reqVO.getPage() == null) {
            reqVO.setPage(1);
        }
        if (reqVO.getPageSize() == null) {
            reqVO.setPageSize(10);
        }
        return voteConsoleService.stat(reqVO);
    }

    @Override
    public Boolean export(VoteExportVO reqVO) {
        return voteConsoleService.export(reqVO);
    }


    @Override
    public List<VoteDetailRspVO> voteDetail(VoteDetailReqVO reqVO) {
        return voteService.voteDetail(reqVO, null);
    }

    @Override
    public PublishRspVO isPublish(VotePublishReqVO reqVO) {
        VoteInfoReqVO vo = new VoteInfoReqVO();
        vo.setVoteId(reqVO.getVoteId());
        VoteInfoRspVO info = voteService.info(vo, TokenThreadLocal.getToken().getAccountId());
        PublishRspVO rspVO = new PublishRspVO();
        rspVO.setPublish(info.getPublish());
        return rspVO;
    }


}
